package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.edu.koTA07.XmlDataSet.CustomerXmlDataSet;
import com.edu.koTA07.bean.CustomerBean;
import com.edu.koTA07.sqlHelper.SQLHelper;
import com.edu.koTA07.takingOrder.R;

public class CustomerUtils {
	private static final String TAG = "cek";
	SQLHelper helper;
	SQLiteDatabase database;	
	
	public CustomerUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public CustomerBean getCustomerById(String IDCustomer){
		CustomerBean customer = new CustomerBean();
		Cursor dataCursor = database.query(helper.TABLE_CUSTOMER, 
				new String[] {helper.ID_CUSTOMER, helper.NAMA_TOKO, helper.NAMA_PEMILIK, 
				helper.TELEPON, helper.TIPE_HARI_KUNJUNGAN, helper.KODE_KUNJUNGAN, helper.ALAMAT_CUSTOMER},   
				helper.ID_CUSTOMER+" =?", new String[]{IDCustomer},null,null,null);
		dataCursor.moveToFirst();
		String idCustomer = dataCursor.getString(0);
		String nameToko = dataCursor.getString(1); 
		String namaPemilik = dataCursor.getString(2);
		String telepon = dataCursor.getString(3); 
		String tipeHariKunjungan = dataCursor.getString(4);
		String kodeKunjungan = dataCursor.getString(5);
		String alamatCustomer = dataCursor.getString(6);
		
		customer.setIDCustomer(idCustomer); 
		customer.setNamaToko(nameToko);
		customer.setNamaPemilik(namaPemilik); 
		customer.setTelepon(telepon); 
		customer.setTipeHariKunjungan(tipeHariKunjungan);
		customer.setKodeKunjungan(kodeKunjungan);
		customer.setAlamatCustomer(alamatCustomer);
		dataCursor.close();
		return customer;
		
	}
	
	@SuppressWarnings("static-access")
	public ArrayList<CustomerBean> loadData() {
		ArrayList<CustomerBean> currentData = new ArrayList<CustomerBean>();
		Cursor dataCursor = database.query(helper.TABLE_CUSTOMER, 
				new String[] {helper.ID_CUSTOMER, helper.NAMA_TOKO, helper.NAMA_PEMILIK, 
				helper.TELEPON, helper.TIPE_HARI_KUNJUNGAN, helper.KODE_KUNJUNGAN, helper.ALAMAT_CUSTOMER}, 
				null, null, null, null, String.format("%s", helper.ID_CUSTOMER));
		//database.q
		
		dataCursor.moveToFirst(); CustomerBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				String idCustomer = dataCursor.getString(0);
				String nameToko = dataCursor.getString(1); 
				String namaPemilik = dataCursor.getString(2);
				String telepon = dataCursor.getString(3); 
				String tipeHariKunjungan = dataCursor.getString(4);
				String kodeKunjungan = dataCursor.getString(5);
				String alamatCustomer = dataCursor.getString(6);
								
				t = new CustomerBean(); 
				t.setIDCustomer(idCustomer); 
				t.setNamaToko(nameToko);
				t.setNamaPemilik(namaPemilik); 
				t.setTelepon(telepon); 
				t.setTipeHariKunjungan(tipeHariKunjungan);
				t.setKodeKunjungan(kodeKunjungan);
				t.setAlamatCustomer(alamatCustomer);
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}

	@SuppressWarnings("static-access")
	public void onSaveDataFromXml(CustomerXmlDataSet customer) {
    	//assert (null != customer);
    	//helper.onCreate(database);
		
		for (int i = 0; i < customer.getNamaToko().size(); i++) {
			ContentValues values = new ContentValues();
			values.put(helper.ID_CUSTOMER, customer.getIDCustomer().get(i));
			values.put(helper.NAMA_TOKO, customer.getNamaToko().get(i));

			Log.d(TAG, customer.getNamaToko().get(i));
			Log.d(TAG, values.toString());
			values.put(helper.NAMA_PEMILIK, customer.getNamaPemilik().get(i));
			values.put(helper.TELEPON, customer.getTelepon().get(i));
			values.put(helper.TIPE_HARI_KUNJUNGAN, customer.getTipeHariKunjungan().get(i));
			values.put(helper.KODE_KUNJUNGAN, customer.getKodeKunjungan().get(i));
			values.put(helper.ALAMAT_CUSTOMER, customer.getAlamatCustomer().get(i));
			//Log.d(TAG, database.toString());
			
			database.insert(helper.TABLE_CUSTOMER, null, values);
    	}//helper.close();
		
    }
	
	AlertDialog alert;
    public void onShowData(CustomerBean cust, Context ctx) {
		final CustomerBean thisCust = cust;		
		alert = new AlertDialog.Builder(ctx).setIcon(R.drawable.icon)
	        .setTitle("Display Data")
	        .setMessage(" ----------- Customer ------------\n"
	        		  + "ID : "+thisCust.getIDCustomer()+"\n" 
	        		  + "Nama Toko : "+thisCust.getNamaToko()+"\n"
	        		  + "Nama Pemilik : "+thisCust.getNamaPemilik()+"\n"
	        		  + "Telepon : "+thisCust.getTelepon()+"\n"
	        		  + "Tipe Hari Kunjungan : "+thisCust.getTipeHariKunjungan()+"\n"
	        		  + "Alamat Customer : "+thisCust.getAlamatCustomer()+"\n")
	        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	               alert.cancel();
	            }
	        }).create();			
		alert.show();
	}

}
